/**
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.biofoco.p2p.bootstrap;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.biofoco.p2p.rpc.torrent.ServerTorrentInitializer;
import br.biofoco.p2p.utils.GuiceHelper;

import com.google.inject.Injector;

public class MainApp {

	private static PeerApp peerApp;
	private static Injector injector = GuiceHelper.getInjector();
	private static JobTracker c;
	private static BlastClient bc;
	private static Logger LOGGER = null;

	public static void main(String[] args) throws Exception {
		String instancia_log = System.getProperty("config.dir", "conf");
		char numero_log = '0';
		if(!instancia_log.equalsIgnoreCase("conf"))
			numero_log = instancia_log.charAt(instancia_log.length() - 1);
		LOGGER = LoggerFactory.getLogger("biofoco" + numero_log);
		ServerTorrentInitializer sti = new ServerTorrentInitializer(LOGGER);
		sti.initilizeServerTorrent();
		peerApp = injector.getInstance(PeerApp.class);
		Set<String> params = new HashSet<String>(Arrays.asList(args));
		if (params.contains("-gui")) {
			String schedule = System.getProperty("schedule", "WQ");
			if(schedule.equalsIgnoreCase("WQ")){
				bc = new BlastClient();
				LOGGER.debug("MainApp.main() - BIOFOCO iniciado com escalonador WQ.");
				peerApp.addListener(bc);
				bc.showWindow();
			} else {
				c = new JobTracker();
				LOGGER.debug("MainApp.main() - BIOFOCO iniciado com escalonador WQR.");
				peerApp.addListener(c);
				c.showWindow();
			}
		}

		peerApp.start();
	}

}